# @Version        : 1.0
# @Update Time    : 2025/3/13 14:31
# @File           : router.py
# @IDE            : PyCharm
# @Desc           : 文件描述信息
from urllib.parse import parse_qs

import flet as ft

from views.cart_view import CartView
from views.category_view import CategoryView
from views.coupon_view import CouponView
from views.favorites_view import FavoritesView
from views.forgot_password_view import ForgotPasswordView
from views.home_view import HomeView
from views.login_view import LoginView
from views.notification_view import NotificationView
from views.order_management_view import OrderView
from views.product_detail_view import ProductDetailView
from views.profile_view import ProfileView
from views.registration_view import RegistrationView
from views.reset_password_view import ResetPasswordView
from views.scan_view import ScanView
from views.search_view import SearchView  # 添加: 导入 SearchView
from views.verification_view import VerificationView


# utils/router.py
class Router:
    def __init__(self, page: ft.Page):
        self.page = page
        self.appbar = ft.AppBar(
            title=ft.Text(
                "生鲜商城", weight=ft.FontWeight.BOLD,
            ),
            bgcolor=ft.colors.GREEN_100,
            center_title=True,
            # 返回上一个页面
            leading=ft.IconButton(
                icon=ft.icons.ARROW_BACK, icon_color="white", on_click=lambda e: self.back(),
            ),
            # 修改: 设置返回图标的图标
            leading_width=40,
            actions=[
                ft.IconButton(
                    icon=ft.icons.SEARCH,
                    icon_color="white",
                    on_click=lambda e: page.go("/search"),  # 修改: 添加跳转到搜索页面的逻辑
                ),
                # 客服按钮
                ft.IconButton(
                    icon=ft.icons.MESSAGE,
                    icon_color="white",
                    # on_click=lambda e: page.go("/chat"),  # 修改: 添加跳转到客服页面的逻辑
                ),
                ft.IconButton(
                    icon=ft.icons.BRIGHTNESS_6_OUTLINED,
                    icon_color="white",
                    on_click=lambda e: self.toggle_theme(),
                ),
            ]
        )
        self.nav_bar = ft.NavigationBar(
            selected_index=0,
            destinations=[
                ft.NavigationBarDestination(icon=ft.icons.HOME, label="首页", data="/"),
                ft.NavigationBarDestination(icon=ft.icons.SCANNER, label="扫描", data="/scan"),
                ft.NavigationBarDestination(icon=ft.icons.SHOPPING_CART, label="购物车", data="/cart"),
                ft.NavigationBarDestination(icon=ft.icons.PERSON, label="我的", data="/profile"),
            ],
            on_change=lambda e: self.page.go(e.control.destinations[e.control.selected_index].data),
        )
        # 路由字典
        self.routes = {
            "/": HomeView,
            "/login": LoginView,
            # 忘记密码
            "/forgot_password": ForgotPasswordView,
            # 验证码校验
            "/verification": VerificationView,
            # 重置密码
            "/change_password": ResetPasswordView,
            # 注册
            "/register": RegistrationView,
            "/scan": ScanView,
            "/cart": CartView,
            "/profile": ProfileView,
            "/search": SearchView,  # 添加: 添加 /search 路由
            # CategoryView
            "/category": CategoryView,
            # 商品详情页面
            "/product_detail": ProductDetailView,
            # 通知设置界面
            "/notification": NotificationView,
            # 优惠卷界面
            "/coupon": CouponView,
            "/favorites": FavoritesView,
            "/order_management": OrderView,
        }
        self.no_nav_routes = ["/login", "/register", "/forgot_password", "/change_password", "/verification"]
        # 历史路由记录
        self.history = []
        self.body = ft.Container(expand=True, alignment=ft.alignment.center)

    def route_change(self, route: ft.RouteChangeEvent):
        self.page.views.clear()
        path, _, query = route.route.partition('?')
        # 将参数放到字典
        params = parse_qs(query)
        self.history.append(path)
        if path in self.routes:
            self.body.content = self.routes[path](self.page, params)
        else:
            self.body.content = ft.Text("404: 页面未找到")
        # 动态控制导航栏
        if path in self.no_nav_routes:
            self.page.views.append(ft.View(path, [self.appbar, self.body]))
        else:
            self.page.views.append(ft.View(path, [self.appbar, self.body, self.nav_bar]))
        # 特殊处理启动页
        self.page.update()

    # 返回上一个路由
    def view_pop(self, view=None):
        if len(self.page.views) > 1:
            self.page.views.pop()
            top_view = self.page.views[-1]
            self.page.go(top_view.route)

    def back(self):
        if len(self.history) > 1:
            self.history.pop()
            self.page.go(self.history[-1])

    def toggle_theme(self):
        self.page.theme_mode = "light" if self.page.theme_mode == "dark" else "dark"
        self.page.update()
